
/*
 *  Source name     : lab0_asmprog.s
 *  Executable name : lab0_asmprog
 *  Version         : 1.0
 *  Created date    : 9/18/03
 *  Last update     : 9/18/03
 *  Author          : Jason Fritts
 *  Description     : Basic template for GNU assembly programs
 * 
 *  Build using these commands:
 *    as --gstabs lab0_asmprog.s -o lab0_asmprog.o
 *    gcc -g -c input.c -o input.o
 *    gcc lab0_asmprog.o input.o -o lab0_asmprog
 */

.equiv MIN_ELEMENTS, 1
.equiv MAX_ELEMENTS, 1000000
.equiv BOUND_WORD, 65536

.text
.global main				/* program entry point */

main:
    pushl %ebp				/* Create stack frame */
    movl  %esp, %ebp 			/* ebp marks our place in the stack */

    pushl %ebx				/* Save ebx, edi, & esi */
    pushl %edi
    pushl %esi

    /* start program code */

    pushl $MAX_ELEMENTS		/* query user for # of elements to print */
    pushl $MIN_ELEMENTS
    call  get_number


    addl  $8, %esp
    movl  %eax, n(,1)

    leal  num_set(,1), %eax

    /* Added code */
   
    pushl $MAX_ELEMENTS		/* query user for # of elements to print */
    pushl $MIN_ELEMENTS
    call get_increment_number
    
    addl  $8, %esp
    movl  %eax, n(,1)
    leal  num_set(,1), %eax


    /* Determine n elements in set */

    movl  $0, %ecx			/* i = 0 */

    i_loop:
	cmpl  n(,1), %ecx		/* is i < n? */
	jae   exit_i_loop
	
	movl  %ecx, %esi
	shll  $2, %esi			/* mult i by 4 to index into num_set */

	movl  %ecx, %ebx
	shll  $2, %ebx			/* compute 2 * i + 1 */
	addl  $1, %ebx

	movl  %ebx, num_set(%esi)	/* num_set[i] = 2 * i + 1 */

	addl  $1, %ecx			/* i = i + 1 */
	jmp   i_loop

    exit_i_loop:


    /* Print out n elements of number pattern */

    pushl n(,1)				/* print "Display X element..." */
    pushl $out_str
    call printf
    addl  $8, %esp

    movl  $0, %ecx			/* i = 0 */

    print_loop:
	cmpl  n(,1), %ecx		/* is i < n? */
	jae   exit_print_loop
	
	movl  %ecx, %esi
	shll  $2, %esi			/* mult i by 4 to index into num_set */

	pushl %ecx			/* save necessary registers before */
	pushl %esi			/*   a procedure call */

	pushl num_set(%esi)		/* print out value of i-th element */
	pushl $elem_str
	call  printf
	addl  $8, %esp

	popl  %esi			/* restore regs after a procedure */
	popl  %ecx			/*   call */

	addl  $1, %ecx			/* i = i + 1 */
	jmp  print_loop

    exit_print_loop:

    pushl $end_line			/* print end of line */
    call  printf
    addl  $4, %esp

    /* end program code */

    popl  %esi				/* Restore ebx, edi, & esi */
    popl  %edi
    popl  %ebx
    movl  %ebp, %esp			/* Destroy stack frame */
    popl  %ebp
    ret					/* Return control to Linux */


.section .rodata			/* read-only (e.g. string) data */

    out_str:
	.string "\nDisplaying %d elements of pattern:\n    "
    elem_str:
	.string "%d "
    end_line:
	.string "\n"

.section data				/* initialized data */

/*    i: */
/*	.word	0 */

.section bss				/* uninitialized data */

	.comm n, 4
	.comm num_set, 4*MAX_ELEMENTS

